﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using vnCommonGUI;
using vnBiometricsSDK.FingerPrint;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
namespace UserControlForFingerprint
{
    public partial class VanTay : UserControl
    {
        string m_socmnd;
        public Bitmap bmp=new Bitmap(512,512);
        public string SoCMND
        {
            get
            {
                return m_socmnd;
            }
            set
            {
                m_socmnd =value;
                if (m_socmnd == "")
                {
                    pbImage.Image = null;
                    btnChangeImage.Enabled = false;
                }
                else
                {
                    btnChangeImage.Enabled = true;
                    if (loaingon != "")
                    {
                        VanTay_Object vo=new VanTay_Object();
                        try
                        {
                           vo = VanTay_DataAccess.GetObject(m_socmnd, loaingon);
                        }
                        catch
                        {
                            vo = null;
                        }
                        if (vo != null)
                        {
                            if (vo.ImgUrl != "") pbImage.Image = new Bitmap(Config_DataAccess.GetVanTayImagePath(vo.ImgUrl));
                        }
                    }
                }
            }
        }

        [CategoryAttribute("ABC"), DescriptionAttribute("Chọn loại ngón tay của ảnh vân tay")]
        [Bindable(true)]
        public string loaingon { get; set; }
        public VanTay()
        {
            InitializeComponent();
            if ((SoCMND == "") ||(SoCMND==null)) btnChangeImage.Enabled = false;
            btnChangeImage.Text = loaingon;
        }

        private void VanTay_Load(object sender, EventArgs e)
        {
            btnChangeImage.Text = loaingon;
        }

        private void btnChangeImage_Click(object sender, EventArgs e)
        {
            if (m_socmnd == "")
            {
                MessageBox.Show("Chưa ghi nhận thông tin công dân");
                return;
            }
            pbImage.Image = CommonFunction.OpenDialogImageFile();
            bmp = (Bitmap)pbImage.Image;
            progressBar1.Visible = true;
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            if (bmp == null) return;
            Bitmap newbmp = new Bitmap(bmp);
            newbmp = vnSumarySDK.CaiThienAnh(newbmp);
            string coreDelta;
            string dactrung1 = vnSumarySDK.LayDacTrungString(newbmp,out coreDelta);
            if (m_socmnd != "" && loaingon != "")
            {
                VanTay_Object vo=new VanTay_Object();
                vo.LoaiNgon=loaingon;
                vo.ImgUrl=SoCMND+"_"+loaingon+".jpg";
                vo.So_CMND=SoCMND;
                vo.DacTrung=dactrung1;
                vo.CoreDelta = coreDelta;
                try
                {
                    bmp.Save(Config_DataAccess.GetVanTayImagePath(vo.ImgUrl));
                    VanTay_DataAccess.Submit(vo);
                }
                catch
                {
                    try
                    {
                        bmp.Save(Config_DataAccess.GetVanTayImagePath(vo.ImgUrl));
                        VanTay_DataAccess.Submit(vo);
                    }
                    catch
                    {
                    }
                }
            }
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Visible = false;
        }

        private void pbImage_MouseHover(object sender, EventArgs e)
        {
           
        }

        private void pbImage_MouseLeave(object sender, EventArgs e)
        {
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (m_socmnd != "" && loaingon != "")
            {
                VanTay_Object vo = new VanTay_Object();
                vo.LoaiNgon = loaingon;
                vo.ImgUrl = SoCMND + "_" + loaingon + ".jpg";
                vo.So_CMND = SoCMND;
                //bmp.Save(Config_DataAccess.GetVanTayImagePath(vo.ImgUrl));
                VanTay_DataAccess.Delete(vo);
                pbImage.Image = null;
            }
        }
    }
}
